﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExplorerPlus.PluginsInterface;
using ExplorerPlus.DomainObjects;
using ExplorerPlus.AmazonApi;
using ExplorerPlus.Plugins.Common;

namespace ExplorerPlus.Plugins.Movies
{
    public class MoviesWebSearchPlugin : IWebSearchPlugin
    {
        #region IWebSearchPlugin Members

        public string[] GetSearchColumns()
        {
            return new string[] { "Title", "Actor", "Production Studio", "ASIN" };
        }

        public List<string[]> Search(string keyword)
        {
            AmazonClient client = new AmazonApi.AmazonClient();
            AmazonApi.AmazonWebService.ItemSearchResponse response =
                client.Search("DVD", keyword);

            List<string[]> result = new List<string[]>();

            if (response.Items != null && response.Items.Length > 0 && response.Items[0].Item != null)
            {
                foreach (var item in response.Items[0].Item)
                {
                    if (item.ItemAttributes.Actor == null)
                    {
                        item.ItemAttributes.Actor = new string[] { "" };
                    }

                    if (item.ItemAttributes.Manufacturer == null)
                    {
                        item.ItemAttributes.Manufacturer = "";
                    }

                    result.Add(
                        new string[] {
                        item.ItemAttributes.Title, 
                        item.ItemAttributes.Actor[0], 
                        item.ItemAttributes.Manufacturer, 
                        item.ASIN});
                }
            }

            return result;
        }

        public List<string[]> SearchAdvanced(string keyword)
        {
            AmazonClient client = new AmazonApi.AmazonClient();
            AmazonApi.AmazonWebService.ItemSearchResponse response =
                client.SearchAdvanced("DVD", keyword);

            List<string[]> result = new List<string[]>();

            if (response.Items != null && response.Items.Length > 0 && response.Items[0].Item != null)
            {
                foreach (var item in response.Items[0].Item)
                {
                    if (item.ItemAttributes.Actor == null)
                    {
                        item.ItemAttributes.Actor = new string[] { "" };
                    }

                    if (item.ItemAttributes.Manufacturer == null)
                    {
                        item.ItemAttributes.Manufacturer = "";
                    }

                    if (item.SmallImage == null)
                    {
                        item.SmallImage = new AmazonApi.AmazonWebService.Image();
                    }

                    result.Add(
                        new string[] {
                        item.ItemAttributes.Title, 
                        item.ItemAttributes.Actor[0], 
                        item.ItemAttributes.Manufacturer, 
                        item.ASIN, item.SmallImage.URL});
                }
            }

            return result;
        }

        public Item FindItem(string[] keywords)
        {
            AmazonClient client = new AmazonApi.AmazonClient();
            AmazonApi.AmazonWebService.ItemLookupResponse response =
                client.FindItemByASIN(keywords[3]);
            if (response == null || response.Items.Length == 0 || response.Items[0].Item == null)
            {
                return null;
            }

            AmazonApi.AmazonWebService.Item item = response.Items[0].Item[0];

            Item movie = new Item((int)Categories.Movies);
            movie.ItemName = item.ItemAttributes.Title;
            movie.AddItemProperty((int)MovieFields.DetailPageUrl, item.DetailPageURL);
            movie.AddItemProperty((int)MovieFields.AudienceRating, item.ItemAttributes.AudienceRating);
            movie.AddItemProperty((int)MovieFields.Binding, item.ItemAttributes.Binding);
            movie.AddItemProperty((int)MovieFields.ProductionStudio, item.ItemAttributes.Manufacturer);
            if (item.ItemAttributes.OriginalReleaseDate != null)
            {
                movie.AddItemProperty((int)MovieFields.ReleaseDate, item.ItemAttributes.OriginalReleaseDate);
            }
            else
            {
                movie.AddItemProperty((int)MovieFields.ReleaseDate, item.ItemAttributes.ReleaseDate);
            }
            if (item.ItemAttributes.RunningTime != null)
            {
                movie.AddItemProperty((int)MovieFields.RunTime,
                    item.ItemAttributes.RunningTime.Value.ToString() + " " + item.ItemAttributes.RunningTime.Units);
            }
            movie.AddItemProperty((int)MovieFields.SalesRank, item.SalesRank);
            if (item.ItemAttributes.Actor != null)
            {
                foreach (string actor in item.ItemAttributes.Actor)
                {
                    movie.AddItemProperty((int)MovieFields.Actor, actor);
                }
            }
            if (item.ItemAttributes.Director != null)
            {
                foreach (string director in item.ItemAttributes.Director)
                {
                    movie.AddItemProperty((int)MovieFields.Director, director);
                }
            }
            if (item.ItemAttributes.Creator != null)
            {
                for (int i = 0; i < item.ItemAttributes.Creator.Length; i++)
                {
                    if (item.ItemAttributes.Creator[i].Role == "Producer")
                    {
                        movie.AddItemProperty((int)MovieFields.Producer,
                            item.ItemAttributes.Creator[i].Value);
                    }
                    else if (item.ItemAttributes.Creator[i].Role == "Writer")
                    {
                        movie.AddItemProperty((int)MovieFields.Writer,
                            item.ItemAttributes.Creator[i].Value);
                    }
                }
            }
            if (item.LargeImage != null)
            {
                movie.AddItemProperty((int)MovieFields.LargeImage, item.LargeImage.URL);
            }
            if (item.ItemAttributes.ListPrice != null)
            {
                movie.AddItemProperty((int)MovieFields.ListPrice, item.ItemAttributes.ListPrice.FormattedPrice);
            }
            if (item.SmallImage != null)
            {
                movie.AddItemProperty((int)MovieFields.SmallImage, item.SmallImage.URL);
            }
            if (item.CustomerReviews != null)
            {
                movie.AddItemProperty((int)MovieFields.Rating, item.CustomerReviews.AverageRating.ToString());
                movie.AddItemProperty((int)MovieFields.TotalCustomerReviews, item.CustomerReviews.TotalReviews);
            }
            if (item.EditorialReviews != null && item.EditorialReviews.Length > 0)
            {
                for (int i = 0; i < item.EditorialReviews.Length; i++)
                {
                    movie.AddItemProperty((int)MovieFields.EditorialReviews, 
                        string.Format("<b>{0}</b><br>{1}", 
                        item.EditorialReviews[i].Source, 
                        item.EditorialReviews[i].Content));
                }
            }
            List<string> catArr = PluginHelper.GetBrowseNodesParent(item.BrowseNodes, "Genres");
            if (!catArr.Contains("All Movies"))
            {
                catArr.Add("All Movies");
            }
            foreach (string cat in catArr)
            {
                movie.AddItemProperty((int)MovieFields.Genre, cat);
            }

            return movie;
        }

        public void DownloadItemImages(Item item, string imagesPath)
        {
            // Add logic to download book images to images folder
            try
            {
                PluginHelper.SaveImageToDisk(item, (int)MovieFields.LargeImage, imagesPath);
                PluginHelper.SaveImageToDisk(item, (int)MovieFields.SmallImage, imagesPath);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
        }

        #endregion
    }
}
